/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rcnss.data;

import java.sql.*;
import java.util.*;


/**
 *
 * @author Guesmi
 */
public class Structure extends ITable {

    private static final String Table = "structure";

    private int STR_ID ;
    private int STR_CODE ;
    private String STR_LIB ;
    private String STR_ADRESSE ;

    public Structure(){}

    public int GetCODE(){return this.STR_CODE;}
    public String GetLIB(){return this.STR_LIB;}
    public int GetIDStructure(){return this.STR_ID;}
    public String GetAdresse(){return this.STR_ADRESSE;}
    public void SetIDStructure(int id){this.STR_ID = id;}
    public void SetAdresse(String adr){this.STR_ADRESSE = adr;}
    public void SetCODE(int code){this.STR_CODE = code ;}
    public void SetLIB(String lib){this.STR_LIB = lib ;}

    /*Functions*/
    public void SaveToDB() throws Exception{
            db.InsertNewLine(Structure.Table,
                    "STR_ID",Integer.toString(this.STR_ID),
                    "STR_CODE",Integer.toString(this.STR_CODE),
                    "STR_ADRESSE","'"+this.STR_ADRESSE+"'",
                    "STR_LIB","'"+this.STR_LIB+"'");
    }

    public void LoadFromDB(String MAT) {
        try{
        ResultSet res = db.executerSelect(" Select * from "+ Structure.Table +" where STR_CODE ='" + MAT + "'");
        if(res.next())
        {
            this.STR_ID = res.getInt("STR_ID");
            this.STR_CODE = res.getInt("STR_CODE");
            this.STR_LIB = res.getString("STR_LIB");
            this.STR_ADRESSE = res.getString("STR_ADRESSE");

        }else
            System.out.println("Erreur au Laod de structure !");
        }catch(Exception sqle){}
    }


      @Override
    public String toString()
    {
        return this.STR_LIB;
    }


    /**************************************************************************
     *                      STATIC FUCTIONS
     **************************************************************************/

    public static Structure SearchByID(int COD){
        Structure str = new Structure();
        ConnexionBD db = new ConnexionBD();
        try{
            ResultSet res = db.executerSelect("select * from "+ Structure.Table +" where STR_CODE ='"+ COD +"'");
            while (res.next())
            {
                str.STR_ID = res.getInt("STR_ID");
                str.STR_CODE = res.getInt("STR_CODE");
                str.STR_LIB = res.getString("STR_LIB");
                str.STR_ADRESSE = res.getString("STR_ADRESSE");
            }
                

        }catch(Exception sqle){str = null;}
        return str;
    }

    public static ArrayList<Structure> SearchByNom(String nom){
        ArrayList<Structure> list = new ArrayList<Structure>();
        ConnexionBD db = new ConnexionBD();
        try{
            ResultSet res = db.executerSelect("select * from "+ Structure.Table +" where STR_LIB ='"+ nom +"'");
            while (res.next())
            {
                Structure str = new Structure();
                str.STR_ID = res.getInt("STR_ID");
                str.STR_CODE = res.getInt("STR_CODE");
                str.STR_LIB = res.getString("STR_LIB");
                str.STR_ADRESSE = res.getString("STR_ADRESSE");
                list.add(str);
            }
                System.out.println("La liste contient :"+ list.size() +" structures .");
            
        }catch(Exception sqle){}
        return list;
    }

    public static ArrayList<Structure> getAll(){
        ArrayList<Structure> list = new ArrayList<Structure>();
        ConnexionBD db = new ConnexionBD();
        try{
            ResultSet res = db.executerSelect("select * from "+ Structure.Table);
            while (res.next())
            {
                Structure str = new Structure();
                str.STR_ID = res.getInt("STR_ID");
                str.STR_CODE = res.getInt("STR_CODE");
                str.STR_LIB = res.getString("STR_LIB");
                str.STR_ADRESSE = res.getString("STR_ADRESSE");
                list.add(str);
            }
                System.out.println("La liste contient :"+ list.size() +" structures .");

        }catch(Exception sqle){}
        return list;
    }


}